package com.gamota.mobi.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gamota.mobi.R;
import com.gamota.mobi.adapter.AggregateListAdapter.RowType;

public class ListHeader implements Item {
	
	public String getUpdatesNumber() {
		return updatesNumber;
	}

	public void setUpdatesNumber(String updatesNumber) {
		this.updatesNumber = updatesNumber;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	private String name;
	private String number;
	private String updatesNumber;
	private boolean isUpdateAll;
	private Typeface robotoItalic;
	private Resources res;
	private UpdateAllClickedListener listener;

    public ListHeader(Context context, String name, boolean isUpdateAll) {
        this.name = name;
        this.isUpdateAll = isUpdateAll;
        res = context.getResources();
        robotoItalic = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.roboto_italic_condensed));
    }
    
    public void setUpdateAllClickListener(UpdateAllClickedListener l){
    	this.listener = l;
    }

	@Override
	public int getViewType() {
		// TODO Auto-generated method stub
		return RowType.HEADER_ITEM.ordinal();
	}

	@Override
	public View getView(LayoutInflater inflater, View convertView) {
		// TODO Auto-generated method stub
		HeaderViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.manage_section_header, null);
            holder = new HeaderViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.manage_header_name);
            holder.number = (TextView) convertView.findViewById(R.id.manage_header_number);
            holder.btnUpdateAll = (TextView) convertView.findViewById(R.id.btn_update_all);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        holder.name.setText(name);
        holder.name.setTypeface(robotoItalic);
        if(isUpdateAll){
        	holder.btnUpdateAll.setVisibility(View.VISIBLE);
        	holder.btnUpdateAll.setText(String.format(res.getString(R.string.update_all), updatesNumber));
        	holder.number.setVisibility(View.GONE);
        	holder.btnUpdateAll.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(listener != null){
						listener.onUpdateAllClicked();
					}
				}
			});
        } else {
        	holder.btnUpdateAll.setVisibility(View.GONE);
        	holder.number.setVisibility(View.VISIBLE);
        	holder.number.setText(number);
        }
        return convertView;
	}
	
	private class HeaderViewHolder {
		TextView name;
		TextView number;
		TextView btnUpdateAll;
	}

	public interface UpdateAllClickedListener {
		public void onUpdateAllClicked();
	}
}
